package com.whfc.ms.api.mach.controller.gscx;

import com.whfc.common.result.ListData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.JSONUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.entity.SysUser;
import com.whfc.mach.dto.AppMachOilLogDTO;
import com.whfc.mach.dto.AppMachOilSheetDTO;
import com.whfc.mach.entity.AppDeviceObdLog;
import com.whfc.mach.entity.AppMachDataLog;
import com.whfc.ms.api.mach.service.MsMachInnerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author sun_guodong
 * @date 2020-12-16
 */
@RestController
@RequestMapping("/ms/api/gscx/mach/inner")
public class MsGscxMachInnerController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsMachInnerService msMachInnerService;


    @GetMapping("statMachDayData")
    public Result statMachData(@RequestParam("machId") Integer machId,
                               @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("计算单台设备的每日数据,machId:{},date:{}", machId, date);
        msMachInnerService.statMachDayData(machId, date);
        return ResultUtil.success();
    }


    @GetMapping("batchStatMachDayData")
    public Result batchStatMachDayData(@DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("批量计算设备的每日数据,date:{}", date);
        msMachInnerService.statMachDayData(date);
        return ResultUtil.success();
    }


    @GetMapping("batchStatObdMachDayData")
    public Result batchStatObdMachDayData(@DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("批量计算obd设备的每日数据,date:{}", date);
        msMachInnerService.statObdMachDayData(date);
        return ResultUtil.success();
    }


    @PostMapping("buildMachData")
    public void buildMachData(@RequestParam("machId") Integer machId,
                              @RequestParam("deviceId") Integer deviceId,
                              @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                              @RequestParam("effic") Double effic) {
        logger.info("编造试点项目的设备运行数据，machId:{},deviceId:{},date:{},effic:{}", machId, deviceId, date, effic);
        msMachInnerService.buildMachData(machId, deviceId, date, effic);
    }

    @PostMapping("/transfer/mach/data")
    public Result transferMachData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                   @RequestParam("srcMachId") Integer srcMachId,
                                   @RequestParam("desMachId") Integer desMachId,
                                   @RequestParam("srcDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date srcDate,
                                   @RequestParam("desDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date desDate,
                                   @RequestParam("lngDiff") Double lngDiff,
                                   @RequestParam("latDiff") Double latDiff) {
        logger.info("转移设备数据,loginUser:{},srcMachId:{},desMachId:{},srcDate:{},desDate:{},lngDiff:{},latDiff:{}",
                user.getUsername(), srcMachId, desMachId, srcDate, desDate, lngDiff, latDiff);
        msMachInnerService.transferData(srcMachId, desMachId, srcDate, desDate, lngDiff, latDiff);
        return ResultUtil.success();
    }


    @PostMapping("/refreshPolygonWKT")
    public Result refreshPolygonWkt(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                    @RequestParam("deptId") Integer deptId) {
        logger.info("刷新设备polygonWKT内部接口,loginUser:{},deptId:{}", user.getUsername(), deptId);
        msMachInnerService.refreshPolygonWKT(deptId);
        return ResultUtil.success();
    }

    @PostMapping("/statMachMonthData")
    public Result statMachMonthData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                    @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("内部接口统计设备每月数据,loginer:{},date:{}",
                user.getUsername(), date);
        msMachInnerService.statMachMonthData(date);
        return ResultUtil.success();
    }


    @PostMapping("/amendMachDeviceData")
    public Result amendMachDeviceData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                      @Validated @RequestBody List<AppMachDataLog> param) {
        logger.info("修正设备硬件数据,loginer:{},param:{}", user.getUsername(), param.toString());
        msMachInnerService.amendMachDeviceData(param);
        return ResultUtil.success();
    }


    @PostMapping("/amendObdDeviceData")
    public Result amendObdDeviceData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                     @Validated @RequestBody AppDeviceObdLog param) {
        logger.info("修正OBD设备硬件数据,loginer:{},param:{}", user.getUsername(), param.toString());
        msMachInnerService.amendObdDeviceData(param);
        return ResultUtil.success();
    }

    @GetMapping("mach/oil/log")
    public Result getMachOilLog(@RequestParam("deviceId") Integer deviceId,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam("startTime") Date startTime,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam("endTime") Date endTime) {

        logger.info("查询硬件油位标定原始数据,deviceId:{},startTime:{},endTime:{}", deviceId, startTime, endTime);
        List<AppMachOilLogDTO> data = msMachInnerService.getMachOilLog(deviceId, startTime, endTime);
        return ResultUtil.success(new ListData<>(data));
    }

    @PostMapping("mach/oil/sheet/{machId}")
    public Result saveMachOilSheet(@PathVariable("machId") Integer machId, @Validated @RequestBody String body) {
        List<AppMachOilSheetDTO> oilSheetDTOList = JSONUtil.parseArray(body, AppMachOilSheetDTO.class);
        logger.info("保存硬件油位标定结果,machId:{},body:{}", machId, oilSheetDTOList.size());
        msMachInnerService.saveMachOilSheet(machId, oilSheetDTOList);
        return ResultUtil.success();
    }

    @GetMapping("mach/oil/sheet/{machId}")
    public Result getMachOilSheet(@PathVariable("machId") Integer machId) {
        logger.info("获取硬件油位标定结果,machId:{}", machId);
        List<?> list = msMachInnerService.getMachOilSheet(machId);
        return ResultUtil.success(new ListData<>(list));
    }
}
